home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / macros / texinfo / info / tilde.c < prev    next >
C/C++ Source or Header  |  1994-01-28  |  10KB  |  380 lines

  1. /* tilde.c -- Tilde expansion code (~/foo := $HOME/foo). */
  2.  
  3. /* This file is part of GNU Info, a program for reading online documentation
  4.    stored in Info format.
  5.  
  6.    This file has appeared in prior works by the Free Software Foundation;
  7.    thus it carries copyright dates from 1988 through 1993.
  8.  
  9.    Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993 Free Software
  10.    Foundation, Inc.
  11.  
  12.    This program is free software; you can redistribute it and/or modify
  13.    it under the terms of the GNU General Public License as published by
  14.    the Free Software Foundation; either version 2, or (at your option)
  15.    any later version.
  16.  
  17.    This program is distributed in the hope that it will be useful,
  18.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  19.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20.    GNU General Public License for more details.
  21.  
  22.    You should have received a copy of the GNU General Public License
  23.    along with this program; if not, write to the Free Software
  24.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25.  
  26.    Written by Brian Fox (bfox@ai.mit.edu). */
  27.  
  28. #if defined (__GNUC__)
  29. #  define alloca __builtin_alloca
  30. #else /* !__GNUC__ */
  31. #  if defined (_AIX)
  32.  #pragma alloca
  33. #  else /* !_AIX */
  34. #    if defined (HAVE_ALLOCA_H)
  35. #      include <alloca.h>
  36. #    endif /* HAVE_ALLOCA_H */
  37. #  endif /* !AIX */
  38. #endif /* !__GNUC__ */
  39.  
  40. #include "tilde.h"
  41. #include <pwd.h>
  42.  
  43. #if !defined (savestring)
  44. #define savestring(x) (char *)strcpy (xmalloc (1 + strlen (x)), (x))
  45. #endif /* !savestring */
  46.  
  47. #if !defined (NULL)
  48. #  define NULL 0x0
  49. #endif
  50.  
  51. #if defined (TEST) || defined (STATIC_MALLOC)
  52. static char *xmalloc (), *xrealloc ();
  53. #else
  54. extern char *xmalloc (), *xrealloc ();
  55. #endif /* TEST || STATIC_MALLOC */
  56.  
  57. /* The default value of tilde_additional_prefixes.  This is set to
  58.    whitespace preceding a tilde so that simple programs which do not
  59.    perform any word separation get desired behaviour. */
  60. static char *default_prefixes[] =
  61.   { " ~", "\t~", (char *)NULL };
  62.  
  63. /* The default value of tilde_additional_suffixes.  This is set to
  64.    whitespace or newline so that simple programs which do not
  65.    perform any word separation get desired behaviour. */
  66. static char *default_suffixes[] =
  67.   { " ", "\n", (char *)NULL };
  68.  
  69. /* If non-null, this contains the address of a function to call if the
  70.    standard meaning for expanding a tilde fails.  The function is called
  71.    with the text (sans tilde, as in "foo"), and returns a malloc()'ed string
  72.    which is the expansion, or a NULL pointer if there is no expansion. */
  73. Function *tilde_expansion_failure_hook = (Function *)NULL;
  74.  
  75. /* When non-null, this is a NULL terminated array of strings which
  76.    are duplicates for a tilde prefix.  Bash uses this to expand
  77.    `=~' and `:~'. */
  78. char **tilde_additional_prefixes = default_prefixes;
  79.  
  80. /* When non-null, this is a NULL terminated array of strings which match
  81.    the end of a username, instead of just "/".  Bash sets this to
  82.    `:' and `=~'. */
  83. char **tilde_additional_suffixes = default_suffixes;
  84.  
  85. /* Find the start of a tilde expansion in STRING, and return the index of
  86.    the tilde which starts the expansion.  Place the length of the text
  87.    which identified this tilde starter in LEN, excluding the tilde itself. */
  88. static int
  89. tilde_find_prefix (string, len)
  90.      char *string;
  91.      int *len;
  92. {
  93.   register int i, j, string_len;
  94.   register char **prefixes = tilde_additional_prefixes;
  95.  
  96.   string_len = strlen (string);
  97.   *len = 0;
  98.  
  99.   if (!*string || *string == '~')
  100.     return (0);
  101.  
  102.   if (prefixes)
  103.     {
  104.       for (i = 0; i < string_len; i++)
  105.     {
  106.       for (j = 0; prefixes[j]; j++)
  107.         {
  108.           if (strncmp (string + i, prefixes[j], strlen (prefixes[j])) == 0)
  109.         {
  110.           *len = strlen (prefixes[j]) - 1;
  111.           return (i + *len);
  112.         }
  113.         }
  114.     }
  115.     }
  116.   return (string_len);
  117. }
  118.  
  119. /* Find the end of a tilde expansion in STRING, and return the index of
  120.    the character which ends the tilde definition.  */
  121. static int
  122. tilde_find_suffix (string)
  123.      char *string;
  124. {
  125.   register int i, j, string_len;
  126.   register char **suffixes = tilde_additional_suffixes;
  127.  
  128.   string_len = strlen (string);
  129.  
  130.   for (i = 0; i < string_len; i++)
  131.     {
  132.       if (string[i] == '/' || !string[i])
  133.     break;
  134.  
  135.       for (j = 0; suffixes && suffixes[j]; j++)
  136.     {
  137.       if (strncmp (string + i, suffixes[j], strlen (suffixes[j])) == 0)
  138.         return (i);
  139.     }
  140.     }
  141.   return (i);
  142. }
  143.  
  144. /* Return a new string which is the result of tilde expanding STRING. */
  145. char *
  146. tilde_expand (string)
  147.      char *string;
  148. {
  149.   char *result, *tilde_expand_word ();
  150.   int result_size, result_index;
  151.  
  152.   result_size = result_index = 0;
  153.   result = (char *)NULL;
  154.  
  155.   /* Scan through STRING expanding tildes as we come to them. */
  156.   while (1)
  157.     {
  158.       register int start, end;
  159.       char *tilde_word, *expansion;
  160.       int len;
  161.  
  162.       /* Make START point to the tilde which starts the expansion. */
  163.       start = tilde_find_prefix (string, &len);
  164.  
  165.       /* Copy the skipped text into the result. */
  166.       if ((result_index + start + 1) > result_size)
  167.     result = (char *)xrealloc (result, 1 + (result_size += (start + 20)));
  168.  
  169.       strncpy (result + result_index, string, start);
  170.       result_index += start;
  171.  
  172.       /* Advance STRING to the starting tilde. */
  173.       string += start;
  174.  
  175.       /* Make END be the index of one after the last character of the
  176.      username. */
  177.       end = tilde_find_suffix (string);
  178.  
  179.       /* If both START and END are zero, we are all done. */
  180.       if (!start && !end)
  181.     break;
  182.  
  183.       /* Expand the entire tilde word, and copy it into RESULT. */
  184.       tilde_word = (char *)xmalloc (1 + end);
  185.       strncpy (tilde_word, string, end);
  186.       tilde_word[end] = '\0';
  187.       string += end;
  188.  
  189.       expansion = tilde_expand_word (tilde_word);
  190.       free (tilde_word);
  191.  
  192.       len = strlen (expansion);
  193.       if ((result_index + len + 1) > result_size)
  194.     result = (char *)xrealloc (result, 1 + (result_size += (len + 20)));
  195.  
  196.       strcpy (result + result_index, expansion);
  197.       result_index += len;
  198.       free (expansion);
  199.     }
  200.  
  201.   result[result_index] = '\0';
  202.  
  203.   return (result);
  204. }
  205.  
  206. /* Do the work of tilde expansion on FILENAME.  FILENAME starts with a
  207.    tilde.  If there is no expansion, call tilde_expansion_failure_hook. */
  208. char *
  209. tilde_expand_word (filename)
  210.      char *filename;
  211. {
  212.   char *dirname;
  213.  
  214.   dirname = filename ? savestring (filename) : (char *)NULL;
  215.  
  216.   if (dirname && *dirname == '~')
  217.     {
  218.       char *temp_name;
  219.       if (!dirname[1] || dirname[1] == '/')
  220.     {
  221.       /* Prepend $HOME to the rest of the string. */
  222.       char *temp_home = (char *)getenv ("HOME");
  223.  
  224.       /* If there is no HOME variable, look up the directory in
  225.          the password database. */
  226.       if (!temp_home)
  227.         {
  228.           extern struct passwd *getpwuid ();
  229.           struct passwd *entry;
  230.  
  231.           entry = getpwuid (getuid ());
  232.           if (entry)
  233.         temp_home = entry->pw_dir;
  234.         }
  235.  
  236.       temp_name = (char *)alloca (1 + strlen (&dirname[1])
  237.                       + (temp_home ? strlen (temp_home) : 0));
  238.       temp_name[0] = '\0';
  239.       if (temp_home)
  240.         strcpy (temp_name, temp_home);
  241.       strcat (temp_name, &dirname[1]);
  242.       free (dirname);
  243.       dirname = savestring (temp_name);
  244.     }
  245.       else
  246.     {
  247.       struct passwd *getpwnam (), *user_entry;
  248.       char *username = (char *)alloca (257);
  249.       int i, c;
  250.  
  251.       for (i = 1; c = dirname[i]; i++)
  252.         {
  253.           if (c == '/')
  254.         break;
  255.           else
  256.         username[i - 1] = c;
  257.         }
  258.       username[i - 1] = '\0';
  259.  
  260.       if (!(user_entry = getpwnam (username)))
  261.         {
  262.           /* If the calling program has a special syntax for
  263.          expanding tildes, and we couldn't find a standard
  264.          expansion, then let them try. */
  265.           if (tilde_expansion_failure_hook)
  266.         {
  267.           char *expansion;
  268.  
  269.           expansion =
  270.             (char *)(*tilde_expansion_failure_hook) (username);
  271.  
  272.           if (expansion)
  273.             {
  274.               temp_name = (char *)alloca (1 + strlen (expansion)
  275.                           + strlen (&dirname[i]));
  276.               strcpy (temp_name, expansion);
  277.               strcat (temp_name, &dirname[i]);
  278.               free (expansion);
  279.               goto return_name;
  280.             }
  281.         }
  282.           /* We shouldn't report errors. */
  283.         }
  284.       else
  285.         {
  286.           temp_name = (char *)alloca (1 + strlen (user_entry->pw_dir)
  287.                       + strlen (&dirname[i]));
  288.           strcpy (temp_name, user_entry->pw_dir);
  289.           strcat (temp_name, &dirname[i]);
  290.         return_name:
  291.           free (dirname);
  292.           dirname = savestring (temp_name);
  293.         }
  294.         endpwent ();
  295.     }
  296.     }
  297.   return (dirname);
  298. }
  299.  
  300.  
  301. #if defined (TEST)
  302. #undef NULL
  303. #include <stdio.h>
  304.  
  305. main (argc, argv)
  306.      int argc;
  307.      char **argv;
  308. {
  309.   char *result, line[512];
  310.   int done = 0;
  311.  
  312.   while (!done)
  313.     {
  314.       printf ("~expand: ");
  315.       fflush (stdout);
  316.  
  317.       if (!gets (line))
  318.     strcpy (line, "done");
  319.  
  320.       if ((strcmp (line, "done") == 0) ||
  321.       (strcmp (line, "quit") == 0) ||
  322.       (strcmp (line, "exit") == 0))
  323.     {
  324.       done = 1;
  325.       break;
  326.     }
  327.  
  328.       result = tilde_expand (line);
  329.       printf ("  --> %s\n", result);
  330.       free (result);
  331.     }
  332.   exit (0);
  333. }
  334.  
  335. static void memory_error_and_abort ();
  336.  
  337. static char *
  338. xmalloc (bytes)
  339.      int bytes;
  340. {
  341.   char *temp = (char *)malloc (bytes);
  342.  
  343.   if (!temp)
  344.     memory_error_and_abort ();
  345.   return (temp);
  346. }
  347.  
  348. static char *
  349. xrealloc (pointer, bytes)
  350.      char *pointer;
  351.      int bytes;
  352. {
  353.   char *temp;
  354.  
  355.   if (!pointer)
  356.     temp = (char *)malloc (bytes);
  357.   else
  358.     temp = (char *)realloc (pointer, bytes);
  359.  
  360.   if (!temp)
  361.     memory_error_and_abort ();
  362.  
  363.   return (temp);
  364. }
  365.  
  366. static void
  367. memory_error_and_abort ()
  368. {
  369.   fprintf (stderr, "readline: Out of virtual memory!\n");
  370.   abort ();
  371. }
  372.  
  373. /*
  374.  * Local variables:
  375.  * compile-command: "gcc -g -DTEST -o tilde tilde.c"
  376.  * end:
  377.  */
  378. #endif /* TEST */
  379.  
  380.